import { generateUniqueId } from "utils/utils";
import { EntityDB } from "../EntityDB";
import { EntityFactory } from "../EntityFactory";
import { EntityType } from "../Entitys/Entity.interface";
 
/**
 * 外框房策略
 */
export class OutMineStrategy
{
    public static check(room:Room,entityDB:EntityDB)
    {
         
        if(room.controller )
        {
            return this.checkConfig(entityDB,room.name );
        }
        return false
    }

    private static checkConfig( entityDB:EntityDB,roomName:string)
    {
        
        const source = global.world.PollSource(roomName);
        if(source)
        {
            for(const e of source.cacheData)
            {
                const  findentity = entityDB.getEntityByID(e.id);
                if(!findentity)
                {
                   
                    const outMineinit:IOutMineInitializer={ 
                        GUID:e.id,
                        key:e.id,
                        roomName:roomName,
                    }  
                  
                    const entity = EntityFactory.CreateModule("OutMineEntity",outMineinit,entityDB);
                    if(entity.region == entity.getSource()?.room.name)
                    {
                        global.Log.Warning(`${roomName} 添加的外矿  ${entity.GUID()} 是本房间`);
                        return false;
                    }
                    entityDB.add(entity);
                }
                
            }
        }
        else
        {
            global.Log.Warning(`${roomName} 查找不到source缓存`);
            return false
        }
        return true
    }
}